﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Net.Mail;

public partial class User_Forgotten_Password : System.Web.UI.Page
{
    MemberBUS memBUS = new MemberBUS();
    DataTable tbMem = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            MultiView1.ActiveViewIndex = 0;
        }
    }
    protected void btnNext_Click(object sender, EventArgs e)
    {
        if (txtEmail.Text.Equals(""))
        {
            lblError.Text = "You must enter Email";
        }
        else
        {
            lblError.Text = "";
            tbMem = memBUS.checkEmail(txtEmail.Text);
            if (tbMem.Rows.Count > 0)
            {
                bool check = memBUS.ForgetPass(txtEmail.Text);
                if (check)
                {
                    SendMail(txtEmail.Text);
                    MultiView1.ActiveViewIndex = 1;
                }
                else
                {
                    lblError.Text = "Error";
                }
            }
            else
            {
                lblError.Text = "Wrong Email";
                txtEmail.Text = "";
            }
        }
    }

    public bool SendMail(string to)
    {

        MailMessage mail = new MailMessage();

        mail.To.Add(to);
        mail.Subject = "Repeat password";
        mail.IsBodyHtml = true;

        string str = "<html>"
                   + "<P>Hello,</P>"
                   + "<P>Thank you for shopping at our website</P>"
                   + "<P>Your password is: 123456</P>"
                   + "</html>";

        mail.Body = str;

        try
        {
            SmtpClient client = new SmtpClient();
            client.EnableSsl = true;
            client.Send(mail);
            return true;
        }
        catch (Exception)
        {
            return false;
        }
    }
}